//
//  QQViewCell.m
//  TestTableView
//
//  Created by Hunk on 12-1-10.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "QQViewCell.h"
#import <QuartzCore/QuartzCore.h>

@implementation QQViewCell
@synthesize addressBookPerson = _addressBookPerson;
@synthesize delegate          = _delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
    {
        _defaultIcon = [[UIImage imageNamed:@"icon_action.png"] retain];
        
//        // Background image view
//        UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 60.0)];
//        [bgImageView setBackgroundColor:[UIColor clearColor]];
//        [bgImageView setImage:[UIImage imageNamed:@"cell_background.png"]];
//        [self addSubview:bgImageView];
//        [bgImageView release];
        
        // Person photo
        _personPhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_personPhotoButton setBackgroundColor:[UIColor clearColor]];
        [_personPhotoButton setFrame:CGRectMake(10.0, 2.0, 56.0, 56.0)];
        _personPhotoButton.layer.cornerRadius = CORNERRADIUS;
        _personPhotoButton.layer.masksToBounds = YES;
        [_personPhotoButton addTarget:self 
                               action:@selector(photoButtonHandle:) 
                     forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_personPhotoButton];
        
        // Person name label
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(70.0, 0.0, 200, 60.0)];
        [_nameLabel setBackgroundColor:[UIColor clearColor]];
        [_nameLabel setFont:[UIFont boldSystemFontOfSize:18]];
        [_nameLabel setTextAlignment:UITextAlignmentLeft];
        [self addSubview:_nameLabel];
        
        // Accessory view
        UIImage *accessoryImage = [UIImage imageNamed:@"accessory.png"];
        UIImageView *accessoryImageView = [[UIImageView alloc] initWithFrame:CGRectMake(280.0, 23.0, accessoryImage.size.width, accessoryImage.size.height)];
        [accessoryImageView setBackgroundColor:[UIColor clearColor]];
        [accessoryImageView setImage:accessoryImage];
        [self addSubview:accessoryImageView];
        [accessoryImageView release];
    }
    return self;
}

#pragma mark - setDelegate
- (void)setDelegate:(id<QQViewCellDelegate>)delegate
{
    if(_delegate != delegate)
    {
        [_delegate release];
    
        _delegate = [delegate retain];
    }
}

#pragma mark - setAddressBookPerson
- (void)setAddressBookPerson:(AddressBookPerson *)addressBookPerson
{
    if(_addressBookPerson != addressBookPerson)
    {
        [_addressBookPerson release];
        
        _addressBookPerson = [addressBookPerson retain];
    }
    
    UIImage *personPhoto = [addressBookPerson personPhoto];
    if(nil != personPhoto)
    {
        [_personPhotoButton setImage:personPhoto forState:UIControlStateNormal];
    }
    else
    {
        [_personPhotoButton setImage:_defaultIcon forState:UIControlStateNormal];
    }
    
    [_nameLabel setText:[_addressBookPerson personName]];
}

#pragma mark - touchesEnded:withEvent:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if([_delegate respondsToSelector:@selector(QQViewCell:didSelected:)])
    {   
        [_delegate QQViewCell:self didSelected:_addressBookPerson];
    }
}

#pragma mark - Photo button handle
- (void)photoButtonHandle:(id)sender
{
    if([_delegate respondsToSelector:@selector(QQViewCell:didSelected:)])
    {   
        [_delegate QQViewCell:self didSelected:_addressBookPerson];
    }
}

- (void)dealloc
{
    SAFE_RELEASE(_defaultIcon)
    SAFE_RELEASE(_nameLabel)
    SAFE_RELEASE(_addressBookPerson)
    SAFE_RELEASE(_delegate)
    
    [super dealloc];
}

@end
